<!--
 * @Descripttion: 核酸宝项目前端
 * @version: 
 * @Author: 杜宇轩
 * @Date: 2022-05-22 05:23:33
 * @LastEditors: 杜宇轩
 * @LastEditTime: 2022-05-22 06:21:31
-->
<template>
  <div class="UUA_box">
    <div class="UUA_box_header">
      <i class="el-icon-menu"/>
      修改申请
    </div>
    <el-page-header
      class="UUA_goBack"
      @back="$router.go(-1)"
    />
    <el-form
      class="UUA_applyForm"
      ref="applyForm"
      :model="applyForm"
      :rules="applyRules"
      label-width="120px"
    >
      <el-form-item label="志愿地点" prop="volunteerLocation">
        <el-cascader
          v-model="applyForm.volunteerLocation"
          :options="options"
          placeholder="请选择志愿地点"
        />
      </el-form-item>
      <el-form-item label="志愿时间" prop="volunteerTime">
        <el-date-picker
          v-model="applyForm.volunteerTime"
          type="date"
          placeholder="请选择志愿日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <el-button
      class="UUA_updateButton"
      type="primary"
      @click="handleUpdateApply"
    >
      修改申请
    </el-button>
  </div>
</template>

<script>
import {
  userUpdateApply
} from '@/api/commonUser/userApply'

export default {
  data() {
    return {
      applyForm: {
        id: '',
        volunteerLocation: [],
        volunteerTime: ''
      },
      applyRules: {
        volunteerLocation: [{ required: true, message: '请选择志愿地点' }],
        volunteerTime: [{ required: true, message: '请选择志愿日期' }]
      },
      options: []
    }
  },
  created() {
    this.$store.dispatch('region/getAllInfo', 1).then((data) => {
      this.options = data;
    });
    const formData = this.$store.getters['commonUser/getCurrentApply'];
    this.applyForm.id = formData.id;
    this.applyForm.volunteerLocation[3] = formData.volunteerLocation;
    this.applyForm.volunteerTime = formData.volunteerTime;
  },
  methods: {
    handleUpdateApply() {
      this.$refs.applyForm.validate(valid => {
        if (valid) {
          const input = {};
          input.id = this.applyForm.id;
          input.volunteerLocation = this.applyForm.volunteerLocation[3];
          input.volunteerTime = this.applyForm.volunteerTime;
          userUpdateApply(input).then(() => {
            this.$message.success('修改成功');
            this.$router.push({ name: 'VolunteerApply' });
          });
        } else {
          console.error('error submit!!!');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss">
.UUA_box {
  background: #FFFFFF;
  display: inline-block;
  width: calc(100% - 100px);
  min-width: 600px;
  margin: 50px 50px 0px 50px;
  border-radius: 5px;
  box-shadow: 1px 1px 3px 3px rgba(0,0,0,0.2);

  .UUA_box_header {
    background: #EEE;
    padding: 5px;
    border-radius: 5px 5px 0px 0px;
    border-bottom: 1px solid #DDD;
  }

  .UUA_goBack {
    margin: 10px;
  }

  .UUA_applyForm {
    width: 80%;
    margin: 50px auto 0px auto;
  }

  .UUA_updateButton {
    position: relative;
    left: calc(50% - min(100px, 10%));
    width: calc(min(200px, 20%));
    margin: 50px 0px 50px 0px;
  }
}

</style>

<style lang="scss" scoped>
.el-cascader {
  width: calc(min(80%, 380px));
}

.el-date-editor {
  width: calc(min(80%, 380px));
}

</style>
